#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"StdThread.h"
#include"DoubleLinkList.h"
#include"ThreadPool.h"
#include"StdTcp.h"

struct Client
{
    char name[20];
    int sock;
};

typedef struct Client C;
C* createC(const char *name,int sock)
{
    C *c=(C*)malloc(sizeof(C));
    if(c==NULL)
    {
        perror("C:");
        return NULL;
    }
    strcpy(c->name,name);
    c->sock=sock;
    return c;
}

void FreeC(C *c)
{
    free(c);
}

DLlist ClientList;

struct Message
{
    int flag;
    char fromName[20];
    char toName[20];
    char content[1024];
};
typedef struct Message Msg;

void *thread_handle(void *arg)
{
    int sock=*((int *)arg);
    while(1)
    {
        Msg m;
        TcpServerRecv(sock,&m,sizeof(m));
        switch(m.flag)
        {
            case 1:
                InsertTail(&ClientList,createC(m.fromName,sock));
                break;
            case 2:
                struct Node *TravelPoint=ClientList.head;
                while(TravelPoint!=NULL)
                {
                    C *c=(C*)TravelPoint->data;
                    if(strcmp(c->name,m.toName)==0)
                    {
                        TcpServerSend(c->sock,&m,sizeof(m));
                        break;
                    }
                    TravelPoint=TravelPoint->next;
                }                
            default:
                break;
        }
    }
}

int main(int argc, char *argv[])
{
    if(argc!=3)
    {
        printf("invalid numbers!\n");
        return -1;
    }
    
    ThreadP *p = InitThreadPool(100,10,50);
    if(p==NULL)
    {
        printf("threadpool is error!\n");
        return -1;
    }
    InitDLinkList(&ClientList);

    TcpS *s=InitTcpServer(argv[1],atoi(argv[2]));
    
    int acceptSock;
    while((acceptSock=TcpServerAccept(s))>0)
    {
        ThreadP_AddTask(p,thread_handle,&acceptSock);
    }
    return 0;
}